package com.zhoujinlong.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.android.core.adapter.RecyclerAdapter;
import com.android.core.ui.BaseSwipeBackActivity;
import com.android.core.widget.SwipeBackLayout;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.zhoujinlong.R;
import com.zhoujinlong.model.bean.ExerciseBean;
import com.zhoujinlong.ui.adapter.ExerciseRecyclerAdapter;
import com.zhoujinlong.ui.widget.TitleBar;
import com.zhoujinlong.utils.BaseUtil;
import com.zhoujinlong.utils.JsonUtils;
import com.zhoujinlong.utils.ResourceUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * author meikoz on 2016/4/19.
 * email  meikoz@126.com
 */
public class ListActivity extends BaseSwipeBackActivity {

    @Bind(R.id.title_bar)
    TitleBar mTitleBar;
    @Bind(R.id.recyclerview_list)
    RecyclerView mRecyclerView;
    private List<ExerciseBean> mExerciseBeanList;
    private ExerciseRecyclerAdapter mAdapter;

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, ListActivity.class);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ButterKnife.bind(this);
        final int type = getIntent().getIntExtra("type", 1);
        initView();
        setDragEdge(SwipeBackLayout.DragEdge.LEFT);
        new getDataTask().execute(type);
    }

    private void initView() {

        mTitleBar.setTitle(getString(R.string.tab_text_home));
        mRecyclerView.setLayoutManager(new LinearLayoutManager(ListActivity.this));
        mExerciseBeanList = new ArrayList<>();
        mAdapter = new ExerciseRecyclerAdapter(ListActivity.this, R.layout.item_guide_list, mExerciseBeanList);
        mAdapter.setOnItemClickListener(mOnItemClickListener);
        mRecyclerView.setAdapter(mAdapter);
    }


    private class getDataTask extends AsyncTask<Integer, Void, String> {
        @Override
        protected String doInBackground(Integer... params) {
            return ResourceUtils.geFileFromAssets(ListActivity.this, BaseUtil.getFileName(BaseUtil.getResourceName(params[0])));
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            dealData(result);
        }

    }


    private RecyclerAdapter.OnItemClickListener mOnItemClickListener = new RecyclerAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(ViewGroup parent, View view, Object exercise, int position) {
            DetailActivity.start(ListActivity.this, (ExerciseBean) exercise);
        }

        @Override
        public boolean onItemLongClick(ViewGroup parent, View view, Object o, int position) {
            return false;
        }
    };


    private void dealData(String result) {
        List<ExerciseBean> exerciseBeanList = null;
        try {
            exerciseBeanList = JsonUtils.deserialize(result, new TypeToken<List<ExerciseBean>>() {
            }.getType());
            if (null != exerciseBeanList) {
                mExerciseBeanList.addAll(exerciseBeanList);
                mAdapter.notifyDataSetChanged();
            }
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
    }
}
